# Title     : TODO
# Objective : TODO
# Created by: yz
# Created on: 2018/6/8
library(optparse)
library(magrittr)
library(tidyr)
library(tibble)
library(tidyverse)

option_list <- list(
  make_option("--t", type = 'character', action = "store", default = "Metabolite", help = "input metabolite ID type"),
  make_option("--d", type = 'character', action = "store", default = "./", help = "the database direcotry"),
  make_option("--base", default = "metabo_base.R", type = "character", help = "metabo base R file"),
  make_option("--g", default = "SampleInfo.csv", type = "character", help = "sample group file")
)
opt <- parse_args(OptionParser(option_list = option_list))

source(opt$base)
mSet <- InitDataObjects("conc", "pathora", FALSE)


sampleInfo <- read.csv(opt$g, header = T, stringsAsFactors = F) %>%
  select(c("SampleID", "ClassNote")) %>%
  as.data.frame()

isMulti <- length(unique(sampleInfo$ClassNote)) > 2
fcData <- data.frame()
if (isMulti) {
  fcData <- read_csv("../../potential/Markers.csv") %>%
    select(c("KEGG")) %>%
    filter(!is.na(KEGG)) %>%
    rowwise() %>%
    do({
         result <- as_tibble(.)
         id <- result[1, "KEGG"]
         ids <- str_split(id, "/") %>% unlist()
         ids %>%
           map_dfr(function(id) {
             tibble(KEGG = id, col = "gold")
           })
       }) %>%
    distinct(KEGG, .keep_all = T)
}else {
  fcData <- read_csv("../../potential/Markers.csv") %>%
    select(c("KEGG", "log2FC")) %>%
    filter(!is.na(KEGG)) %>%
    mutate(col = ifelse(log2FC > 0, "red", "DeepSkyBlue")) %>%
    select(c("KEGG", "col"))
  fcDataNoRatio <- fcData %>%
    filter({
             sizes <- KEGG %>%
               map_dbl(function(id) {
                 str_split(id, "/") %>% unlist() %>% length()
               })
             sizes == 1
           })
  fcDataWithRatio <- fcData %>%
    filter({
             sizes <- KEGG %>%
               map_dbl(function(id) {
                 str_split(id, "/") %>% unlist() %>% length()
               })
             sizes == 2
           }) %>%
    rowwise() %>%
    do({
         result <- as_tibble(.)
         id <- result[1, "KEGG"]
         beforeCol <- result[1, "col"] %>% as.character()
         ids <- str_split(id, "/") %>% unlist()
         ids %>%
           map_dfr(function(id) {
             idData <- fcDataNoRatio %>%
               filter(KEGG == id)
             col <- if (nrow(idData) > 0)NA else "lightgrey"
             tibble(KEGG = id, col = col)
           })
       }) %>%
    filter(!is.na(col)) %>%
    distinct(KEGG, .keep_all = T)
  fcData <- fcDataNoRatio %>%
    rbind(fcDataWithRatio)
  print(fcData %>% as.data.frame())
}
write_tsv(fcData, "fc_color.txt")
head(fcData)


diffData <- read_csv("../../potential/Markers.csv")

if (nrow(diffData) == 0) {
  quit(status = 0)
}

diffKeggs <- diffData %>%
  .$KEGG %>%
  map(function(x) {
    str_split(x, "/") %>% unlist()
  }) %>%
  flatten_chr()
diffKeggs


cmpd.vec <- diffKeggs
mSet <- Setup.MapData(mSet, cmpd.vec)
mSet <- CrossReferencing(mSet, "kegg", dataDir = opt$d)
mSet <- CreateMappingResultTable(mSet, dataDir = opt$d)
save(mSet, file = "idMapping_mSet.RData")

